package com.dycn.posdashboard.listener;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dycn.posdashboard.dao.PosTransactionDataDao;
import com.dycn.posdashboard.dbhelper.DataBaseUser;
import com.dycn.posdashboard.services.PosTradeServices;
import com.dycn.posdashboard.services.SyncTransactionDataServices;

public class SystemInitListener implements ServletContextListener {

	    protected final Log log = LogFactory.getLog(getClass());
	    
	    public static String  ROOT_FOLDER;
	    private static String  FULLSEARCH_INDEX_FOLDER;
	    public static Long 	DEFAULT_INDEXU_PDATE_INTERVAL;
	    public static Long 	DEFAULT_COLUMNDEFIN_UPDATE_INTERVAL;
	    
	    public static String QUERYTRADE_POS_URL;
	    public static String SYNC_POS_TRADE_QUARTZ;
	    
	    public static Long LIVE_CHART_INTERVAL;
	   
	    public void contextDestroyed(ServletContextEvent sce) {

	    }

	    public void contextInitialized(ServletContextEvent contextEvt) {
	        WebApplicationContext applicationContext = WebApplicationContextUtils
	            .getRequiredWebApplicationContext(contextEvt.getServletContext());
	        initSystemParamaters();
	        
	        SyncTransactionDataServices syncTransactionDataServices = (SyncTransactionDataServices) applicationContext.getBean("syncTransactionDataServices");
	        PosTransactionDataDao somePosTransactionDataDao=(PosTransactionDataDao) applicationContext.getBean("posTransactionDataDao");
	        
	        PosTradeServices somePosTradeServices=(PosTradeServices)applicationContext.getBean("posTradeServices");
	        somePosTradeServices.getBranch2MerchantMappingInfo(DataBaseUser.MAIN_USER);
	        
	        somePosTransactionDataDao.systemInit(DataBaseUser.MAIN_USER);
	        log.info("预同步最新的数据导本地.....");
	        syncTransactionDataServices.syncTransactionWithRemotePOSSystem("");
	        log.info("预同步最新的数据导本地.....done");
	        
	        //	        if (res == 1) {
//	            log.debug("Web Init Success....");
//	            //启动定时执行任务
	        	log.info("启动定时执行任务");
	            syncTransactionDataServices.startPosDataSyncJob();
	            log.info("启动定时执行任务....done");
//	            
//	            log.debug("Web Init Success....startTask done");
//	        } else {
//	            log.debug("Web Init failing....");
//	        }
	        
	    }
	    
	    private void initSystemParamaters(){
	    	 Properties pop = new Properties();
	         try {
	             pop.load(this.getClass().getResourceAsStream("/cfg.properties"));
	         } catch (FileNotFoundException e) {
	             e.printStackTrace();
	             log.error("加载配置文件cfg.properties出错");
	         } catch (IOException e) {
	             e.printStackTrace();
	             log.error("加载配置文件cfg.properties出错");
	         }
	         log.info("加载配置文件cfg.propertiesDone~~~~");
	         ROOT_FOLDER = pop.getProperty("download.root.folder");

	         FULLSEARCH_INDEX_FOLDER = pop.getProperty("luceneIndex.folder");
	         log.info("FULLSEARCH_INDEX_FOLDER~~~~"+FULLSEARCH_INDEX_FOLDER); 
	         String termp=pop.getProperty("default.index.update.interval");
	         if(termp==null||termp.trim().equals("")){
	        	 DEFAULT_INDEXU_PDATE_INTERVAL=300000L;
	         }else{
	        	 DEFAULT_INDEXU_PDATE_INTERVAL=(Long.parseLong(termp)<=30000?300000L:Long.parseLong(termp));
	         }
	         
	         log.info("DEFAULT_INDEXU_PDATE_INTERVAL~~~~"+DEFAULT_INDEXU_PDATE_INTERVAL);
	         
	         termp=pop.getProperty("default.columnsDefine.update.interval");
	         if(termp==null||termp.trim().equals("")){
	        	 DEFAULT_COLUMNDEFIN_UPDATE_INTERVAL=300000L;
	         }else{
	        	 DEFAULT_COLUMNDEFIN_UPDATE_INTERVAL=(Long.parseLong(termp)<=30000?300000L:Long.parseLong(termp));
	         }
	         
	         termp=pop.getProperty("QUERYTRADE_POS_URL");
	         if(termp==null||termp.trim().equals("")){
	        	 QUERYTRADE_POS_URL="http://pay.sanwing.com/ws/business/queryTrade";
	         }else{
	        	 QUERYTRADE_POS_URL=termp;
	         }
	         log.error("加载配置文件QUERYTRADE_POS_URL:"+QUERYTRADE_POS_URL);
	         
	         termp=pop.getProperty("SYNC_POS_TRADE_QUARTZ");
	         if(termp==null||termp.trim().equals("")){
	        	 SYNC_POS_TRADE_QUARTZ="0 0/3 * * * ?";
	         }else{
	        	 SYNC_POS_TRADE_QUARTZ=termp;
	         }
	         
	         
	         log.error("加载配置文件QUERYTRADE_POS_URL timer:"+SYNC_POS_TRADE_QUARTZ);
	        
	         termp=pop.getProperty("LIVE_CHART_INTERVAL");
	         if(termp==null||termp.trim().equals("")){
	        	 LIVE_CHART_INTERVAL=60000L;
	         }else{
	        	 LIVE_CHART_INTERVAL=Long.parseLong(termp);
	         }
	         log.error("加载配置文件LIVE_CHART_INTERVAL :"+LIVE_CHART_INTERVAL);
		        
	         
	         //System.out.println(new File(getClass().getResource("/").getFile()+"../../lastsync23"));

	    }
	    
	    public static String getFullSearchIndexFolder(){
	    	return FULLSEARCH_INDEX_FOLDER;
	    }
}
